﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_ViewProviderProposalFormDetailPrintPreview : System.Web.UI.Page
{
    string PageName = "CXVSTM";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();

        if (!IsPostBack)
        {
            if (Request.Params["FormType"] != null)
            {
                if (Request.Params["FormType"] == "CI")
                {
                    lblFormTitle.Text = "Car Insurance";
                }
                else if (Request.Params["FormType"] == "HL")
                {
                    lblFormTitle.Text = "Home Loan";
                }
                else if (Request.Params["FormType"] == "PA")
                {
                    lblFormTitle.Text = "Personal Accident";
                }
                else if (Request.Params["FormType"] == "TA")
                {
                    lblFormTitle.Text = "Travel Insurance";
                }
            }

            if (Request.Params["Date"] != null)
            {
                txtModifiedDate.Text = Request.Params["Date"].ToString();
                string _strDate = txtModifiedDate.Text;
                if (_strDate.Length < 4)
                {
                    txtModifiedDate.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
                    lblDate.Text = txtModifiedDate.Text + "/ALL MONTH";
                }
                else if (_strDate.Length == 4)
                {
                    lblDate.Text = _strDate + "/ALL MONTH";
                }
                else if (_strDate.Length == 6)
                {
                    lblDate.Text = _strDate.Substring(0, 4).ToString();
                    lblDate.Text += "/" + _strDate.Substring(4, 2).ToString();
                }
                else if (_strDate.Length == 8)
                {
                    lblDate.Text = _strDate.Substring(0, 4).ToString();
                    lblDate.Text += "/" + _strDate.Substring(4, 2).ToString();
                    lblDate.Text += "/" + _strDate.Substring(6, 2).ToString();
                }
                else
                {
                    txtModifiedDate.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
                    lblDate.Text = txtModifiedDate.Text + "/ALL MONTH";
                }

                lblPrintDate.Text = CE.Global.General.GetUTCDateTime().ToShortDateString() + " " + CE.Global.General.GetUTCDateTime().ToShortTimeString();
            }
            ShowCXCompanyInformation();
            ShowProviderCompanyInformation();
            //LoadProviderCoverageTypeList();
            //ddlCXCoverageType.SelectedIndex = 0;
            sqlDsProposalFormDetail.DataBind();
            grvResultViewSummary.DataBind();
        }
    }

    protected void ShowCXCompanyInformation()
    {
        CE.Company.CompanyInformation _company = new CE.Company.CompanyInformation();
        CE.Company.CompanyInformationDB _companydb = new CE.Company.CompanyInformationDB();
        string _strCompanyInformation = "";
        _company = _companydb.GetCompanyInformation(1);
        if (_company != null)
        {
            
            _strCompanyInformation += "<strong>" + _company.CompanyName + "</strong>";
            _strCompanyInformation += "<br />" + _company.Address1;
            _strCompanyInformation += "<br />" + _company.Address2;
            _strCompanyInformation += "<br />" + _company.Country;
            _strCompanyInformation += " " + _company.PostCode;
            _strCompanyInformation += "<br />Tel : " + _company.Telephone;
            _strCompanyInformation += "<br />Email :" + _company.EmailAddress;
        }
        lblCompanyInformation.Text = _strCompanyInformation; 
    }

    protected void ShowProviderCompanyInformation()
    {
        CE.Provider.Company _company = new CE.Provider.Company();
        CE.Provider.CompanyDB _companydb = new CE.Provider.CompanyDB();
        string _strCompanyInformation = "";
        Int64 _providercompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
        _company = _companydb.GetCompany(_providercompanyId);
        if (_company != null)
        {
            _strCompanyInformation += "<strong>" + _company.CompanyName + "</strong>";
            _strCompanyInformation += "<br />" + _company.Street;
            //if(_company.City != string.Empty)
            //    _strCompanyInformation += "<br />" + _company.City;
            _strCompanyInformation += "<br />" + _company.City;
            _strCompanyInformation += " " +_company.PostCode;
            _strCompanyInformation += "<br />Tel : " + _company.PhoneNo1;
            _strCompanyInformation += "<br />Email :" + _company.Email1;
        }
        lblProviderCompanyInformation.Text = _strCompanyInformation;
    }
    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/ProposalForm/ViewProviderProposalFormDetail.aspx");
    }

    // private void LoadProviderCoverageTypeList()
    //{
    //    CarCoverTypeList _carcovertypelist = new CarCoverTypeList();
    //    CarCoverTypeDB _carcovertypedb = new CarCoverTypeDB();

    //    _carcovertypelist = _carcovertypedb.GetProviderCoverTypeList(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));

    //    if (_carcovertypelist != null)
    //    {
    //        for (int i = 0; i < _carcovertypelist.Count; i++)
    //        {
    //            ddlCXCoverageType.Items.Add(new ListItem(_carcovertypelist[i].ProviderCoverTypeDescription, _carcovertypelist[i].CXCoverTypeId.ToString()));
    //        }
    //    }

    //    ddlCXCoverageType.Items.Insert(0, new ListItem("ALL", "0"));
    //}

    protected void ImageButton1_Click(object sender, EventArgs e)
    {
        //txtModifiedDate.Text = " ";
        //if (ddlYear.SelectedIndex > 0)
        //{
        //    txtModifiedDate.Text = ddlYear.Text;
        //    if (ddlMonth.SelectedIndex > 0)
        //    {
        //        txtModifiedDate.Text += ddlMonth.Text;
        //        if (ddlDay.SelectedIndex > 0)
        //        {
        //            txtModifiedDate.Text += ddlDay.Text;
        //        }
        //        else
        //        {
        //            ddlDay.SelectedIndex = 0;
        //        }
        //    }
        //    else
        //    {
        //        ddlMonth.SelectedIndex = 0;
        //        ddlDay.SelectedIndex = 0;
        //    }
        //}
        //else
        //{
        //    ddlMonth.SelectedIndex = 0;
        //    ddlDay.SelectedIndex = 0;
        //}
        //grvResultViewSummary.DataBind();
    }

    protected void grvResultViewSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblRefNo = (Label)e.Row.FindControl("lblRefNo");

            Int64 _refId = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "RefID"));
            string _formtype = DataBinder.Eval(e.Row.DataItem, "FormType").ToString();
            DateTime _modDateTime = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "ModifiedDate"));
            
            if (lblRefNo != null)
            {
                string strModDateTime = _modDateTime.Year.ToString("0000") + _modDateTime.Month.ToString("00") + _modDateTime.Day.ToString("00");
                //blRefNo.Text = _formtype + strModDateTime + _refId.ToString();
                lblRefNo.Text = GetReferenceNumber(_refId.ToString());
            }

            float _premium = Convert.ToSingle(DataBinder.Eval(e.Row.DataItem, "Premium"));
            float _toppremium = Convert.ToSingle(txtHidTotalAmount.Text);
            _toppremium = _toppremium + _premium;
            txtHidTotalAmount.Text = _toppremium.ToString();
            lblTotalAmount.Text = "Total Premium Amount : " + _toppremium.ToString("$#,000.00");
        }
    }

    public string GetReferenceNumber(string Number)
    {
        string instype = Request.Params["FormType"].ToString();
        string convertno = General.GetRefNumber(Convert.ToInt64(Number.ToString()));
        string _RefNo = "";
        if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
        {
            if (instype == "PA")
            {
                _RefNo = "PAH" + convertno.ToString();
            }
            else if (instype == "TA")
            {
                _RefNo = "TIH" + convertno.ToString();
            }
            else
            {
                _RefNo = "H" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "30")
        {
            _RefNo = "E" + convertno.ToString();
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "29")
        {
            if (instype == "PA")
            {
                _RefNo = "PAL" + convertno.ToString();
            }
            else if (instype == "TA")
            {
                _RefNo = "TIL" + convertno.ToString();
            }
            else
            {
                _RefNo = "L" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "24")
        {
            if (instype == "TA")
            {
                _RefNo = "TIR" + convertno.ToString();
            }
            else
            {
                _RefNo = "R" + convertno.ToString();
            }
        }
        return _RefNo;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

}
